package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.entity.EBuyGoods;
import com.chhuang.ewcrm.entity.ESellGoods;
import com.chhuang.ewcrm.entity.ETransfer;
import com.chhuang.ewcrm.service.EBuyGoodsService;
import com.chhuang.ewcrm.service.ESellGoodsService;
import com.chhuang.ewcrm.service.ETransferService;
import com.chhuang.ewcrm.string.MoneyUtil;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * (ETransfer)表控制层
 *
 * @author chhuang
 * @since 2020-11-16 09:11:30
 */
@RestController
@RequestMapping("/service/eTransfer")
public class ETransferController {
    Logger logger = LoggerFactory.getLogger(ETransferController.class);
    /**
     * 服务对象
     */
    @Resource
    private ETransferService eTransferService;
    @Resource
    private EBuyGoodsService eBuyGoodsService;
    @Resource
    private ESellGoodsService eSellGoodsService;

    /**
     * 确认记录完成
     * @param transferId
     * @param remark2
     * @return
     */
    @PostMapping("/confirmCompletion")
    public ResultBean<?> confirmCompletion(String transferId, String remark2){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);

        if(StringUtils.isNotEmpty(transferId)) {
            ETransfer eTransfer = this.eTransferService.queryById(transferId);
            if (eTransfer != null) {
                eTransfer.setRemark2(remark2);
                eTransfer.setStatus(2);
                Date now = new Date();
                eTransfer.setModifyTime(now);
                this.eTransferService.update(eTransfer);
                resultBean.setSuccess(true);

                EBuyGoods buyGoods = eBuyGoodsService.queryById(eTransfer.getGoodsId());
                if(buyGoods!=null){
                    buyGoods.setBuyTotalWeight(buyGoods.getBuyTotalWeight()+eTransfer.getNetWeight());
                    buyGoods.setBuyTotalAmount(buyGoods.getBuyTotalAmount()+eTransfer.getPaymentAmount());
                    buyGoods.setBuyAveragePrice(buyGoods.getBuyTotalAmount()/buyGoods.getBuyTotalWeight());
                    buyGoods.setModifyTime(now);
                    eBuyGoodsService.update(buyGoods);
                }else {
                    logger.error("添加的新采购记录，对应的物品已经不存在");
                }

                ESellGoods sellGoods = eSellGoodsService.queryById(eTransfer.getGoods1Id());
                if(sellGoods!=null){
                    sellGoods.setSellTotalWeight(sellGoods.getSellTotalWeight()+eTransfer.getNetWeight());
                    sellGoods.setSellTotalAmount(sellGoods.getSellTotalAmount()+eTransfer.getPaymentAmount());
                    sellGoods.setSellAveragePrice(sellGoods.getSellTotalAmount()/sellGoods.getSellTotalWeight());
                    sellGoods.setModifyTime(now);
                    eSellGoodsService.update(sellGoods);
                }else {
                    logger.error("添加的新销售记录，对应的物品已经不存在");
                }


            }else {
                resultBean.setMessage("称重记录不存在");
            }
        }else {
            resultBean.setMessage("参数不为空");
        }

        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param transferId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<ETransfer> getOne(String transferId) {
        ResultBean<ETransfer> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(transferId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.eTransferService.queryById(transferId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<ETransfer>> query(@RequestBody QueryBean<ETransfer> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<ETransfer>> resultBean = new ResultBean<>();

        PageInfo<ETransfer> pageInfo = null;
        List<ETransfer> transfers = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = eTransferService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = eTransferService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }
}